﻿using BaseDal;
using Models;
using System;
using System.Collections.Generic;
using System.Data;

namespace BaseBll
{
    public class PurchaseAlterBll
    {
        private static readonly Lazy<PurchaseAlterBll> dal = new Lazy<PurchaseAlterBll>(() => new PurchaseAlterBll());
        public static PurchaseAlterBll Instance { get; } = dal.Value;

        private PurchaseAlterBll()
        { }

        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool IsExist(string strWhere)
        {
            return PurchaseAlterDal.Instance.IsExist(strWhere);
        }

        /// <summary>
        /// 查询分页数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strWhere"></param>
        /// <param name="filedOrder"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public DataTable GetPageList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return PurchaseAlterDal.Instance.GetPageList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<PurchaseAlterHead> GetList(string strWhere)
        {
            return PurchaseAlterDal.Instance.GetList(strWhere);
        }

        /// <summary>
        /// 根据id查询实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PurchaseAlterHead GetModelById(int id)
        {
            return PurchaseAlterDal.Instance.GetModelById(id);
        }

        /// <summary>
        /// 根据主表ID查询所有物品
        /// </summary>
        /// <param name="headID"></param>
        /// <returns></returns>
        public List<PurchaseAlterBody> GetItemList(int headID)
        {
            return PurchaseAlterDal.Instance.GetItemList(headID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool AddPurchaseAlter(PurchaseAlterHead model)
        {
            return PurchaseAlterDal.Instance.AddPurchaseAlter(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdatePurchaseAlter(PurchaseAlterHead model)
        {
            return PurchaseAlterDal.Instance.UpdatePurchaseAlter(model);
        }

        /// <summary>
        /// 删除一条数据，及子表所有相关数据
        /// </summary>
        public bool DeletePurchaseAlter(int ID)
        {
            return PurchaseAlterDal.Instance.DeletePurchaseAlter(ID);
        }

        /// <summary>
        /// 修改状态（0：审批中；1：已审批；2：暂存）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UpdateStatus(int id, int status)
        {
            return PurchaseAlterDal.Instance.UpdateStatus(id, status);
        }

        /// <summary>
        /// 验证model各字段合法性
        /// </summary>
        /// <param name="model"></param>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        public bool VerifyModel(PurchaseAlterHead model, ref string msg)
        {
            string strWhere;
            if (model.ID > 0)
            {
                strWhere = " and PA_Number='" + model.PA_Number + "' and ID !=" + model.ID;
            }
            else
            {
                strWhere = " and PA_Number='" + model.PA_Number + "'";
            }
            if (IsExist(strWhere))
            {
                msg = "变更编号已存在";
                return false;
            }

            // 获取公司名称
            Department company = DeptBll.Instance.GetModelById(model.PA_CompanyId);
            if (company != null)
            {
                model.PA_CompanyName = company.FullName;
            }
            else
            {
                msg = "所选公司不存在";
                return false;
            }

            // 获取部门名称
            Department dept = DeptBll.Instance.GetModelById(model.PA_DeptId);
            if (dept != null)
            {
                model.PA_DeptName = dept.FullName;
            }
            else
            {
                msg = "所选部门不存在";
                return false;
            }

            if (model.PA_OrderID <= 0)
            {
                msg = "请选择采购订单";
                return false;
            }

            // 根据订单ID获取订单对象
            List<PurchaseOrderHead> poHModelList = PurchaseOrderBll.Instance.GetList(" and ID = " + model.PA_OrderID);
            PurchaseOrderHead poHModel = null;
            if (poHModelList != null && poHModelList.Count > 0)
            {
                poHModel = poHModelList[0];
                model.PA_OrderNo = poHModel.PO_OrderNo;
                model.PA_SupplierId = poHModel.PO_SupplierId;
                model.PA_SupplierName = poHModel.PO_SupplierName;
                model.PA_ReceivingInfo = poHModel.PO_ReceivingInfo;
            }
            else
            {
                msg = "所选采购订单不存在";
                return false;
            }

            return true;
        }
    }
}